package com.item.service;

import com.item.dao.CategoryMapper;
import com.item.dao.model.CartKey;
import com.item.dao.model.Category;
import com.item.dao.model.CategoryExample;
import com.item.dao.model.Category;
import com.item.dao.model.CategoryExample;

import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    private static final Logger logger = Logger.getLogger(CategoryService.class);

    public int countByExample(CategoryExample example) {
        return this.categoryMapper.countByExample(example);
    }

    public Category selectByPrimaryKey(String id) {
        return this.categoryMapper.selectByPrimaryKey(id);
    }

    public List<Category> selectByExample(CategoryExample example) {
        return this.categoryMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(String id) {
        return this.categoryMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(Category record) {
        return this.categoryMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Category record) {
        return this.categoryMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(CategoryExample example) {
        return this.categoryMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Category record, CategoryExample example) {
        return this.categoryMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Category record, CategoryExample example) {
        return this.categoryMapper.updateByExample(record, example);
    }

    public int insert(Category record) {
        return this.categoryMapper.insert(record);
    }

    public int insertSelective(Category record) {
        return this.categoryMapper.insertSelective(record);
    }
    public String getLeastCode(String pid) {
    	String res = "C";
    	CategoryExample example = new CategoryExample();
    	example.createCriteria().andPidEqualTo(pid);
    	example.setOrderByClause("code ASC");
    	List<Category> list = this.categoryMapper.selectByExample(example);
    	int i = 0;
    	for(int len = list.size(); i < 100 && i < len; i++) {
    		Category s = list.get(i);
    		int sCodeInt = Integer.parseInt(s.getCode().substring(1));
    		if(i < sCodeInt) {
    			break;
    		}
    	}
    	res += i < 10 ? "0" + i : i + "";
//    	if(i == 0)
//    		res += "01";
//    	else
//    		res += i < 10 ? "0" + i : i + "";
    	
    	return res;
    }
}